new Vue({
    el: "#container",
    data() {
        return {
            indentSearch: {
                pageIndex: 0,
                limit: 50,
                total: 0,
                dateFormat: "yyyy-MM-dd HH:mm",
                startDate: null,
                endDate: null,
                searchVal:"",
                orgId: null,
            },
            //  表数据
            indentDatas: [],
            indentLoading: false,
            //  发货单搜索条件
            invoiceSearch: {
                orgId: null,
                //  任务单id
                indentId: null,
                //  是否已经审核
                isExamine: -1,
                //  分页数据
                pageIndex: 0,
                limit: 50,
                total: 0,
                startDate: null,
                endDate: null,
            },
            //  表数据
            invoiceDatas: [],
            invoiceLoading: false,
            //  消耗记录
            invExpendDialog: {
                show: false,
                id: null,
            },
            //  详情数据
            detailDialog: {
                show: false,
                invoiceId: false,
            },
        }
    },
    // 在模板渲染成html前调用
    created() {
        var that = this;
        let currentDate = new Date();
        currentDate.setHours(8);
        currentDate.setMinutes(30);
        currentDate.setSeconds(0);
        let endTime = getFormatDate(currentDate, that.indentSearch.dateFormat);
        let startTime = currentDate.getDate() - 1;
        startTime = currentDate.setDate(startTime);
        startTime = new Date(startTime);
        startTime = getFormatDate(startTime, that.indentSearch.dateFormat);
        that.indentSearch.endDate = endTime;
        that.indentSearch.startDate = startTime;
        that.searchIndent();
    },
    // 在模板渲染成html后调用，通常是初始化页面完成后，再对html的dom节点进行一些需要的操作
    mounted() {
        var that = this;
        // that.indentSearch.startTime = currentDate.setDate(currentDate.getDate() - 2);
    },
    methods: {
        searchIndent() {
            let that = this;
            that.indentSearch.pageIndex = 0;
            that.indentSearch.limit = 50;
            that.loadIndentDatas();
        },
        // 加载数据
        loadIndentDatas: function () {
            let that = this;
            that.indentLoading = true;
            $.ajax({
                type: "POST",
                url: "/pro/indent/page",
                async: true,
                dataType: 'json',
                data: that.indentSearch,
                success: function (res) {
                    if (res.code == 0) {
                        let page = res.data;
                        that.indentDatas = page.records;
                        that.indentSearch.total = page.total;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询发货单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.indentLoading = false;
                }
            })
        },
        //  任务单变动
        indentSelChange(indent) {
            let that = this;
            let indentCode = indent.code;
            that.invoiceSearch.orgId = that.indentSearch.orgId;
            that.invoiceSearch.searchVal = indentCode;
            that.invoiceSearch.pageIndex = 0;
            that.loadInvoiceDatas();
        },
        //  加载发货单数据
        loadInvoiceDatas: function () {
            let that = this;
            let indentCode = that.invoiceSearch.searchVal;
            if (indentCode == null) {
                that.$message({
                    showClose: true,
                    message: '请先选择任务单',
                    type: 'error'
                });
                return;
            }
            that.invoiceLoading = true;
            that.invoiceSearch.startDate = that.indentSearch.startDate;
            that.invoiceSearch.endDate = that.indentSearch.endDate;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/page",
                async: true,
                dataType: 'json',
                data: that.invoiceSearch,
                success: function (res) {
                    if (res.code == 0) {
                        let allData = res.data;
                        that.invoiceDatas = allData.records;
                        that.invoiceSearch.total = allData.total;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询发货单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.invoiceLoading = false;
                }
            })
        },
        /**
         * 显示发货单详情
         * @param invoiceId
         */
        showInvDetail(invoiceId) {
            let that = this;
            that.detailDialog.show = true;
            that.detailDialog.invoiceId = invoiceId;
        },
        // 修改每页数据
        indentSizeChange(val) {
            var that = this;
            that.indentSearch.limit = val;
            // 查询数据
            that.loadIndentDatas();
        },
        indentCurrentChange(val) {
            // 设置页数
            var that = this;
            that.indentSearch.pageIndex = val;
            that.loadIndentDatas();
        },
        // 修改每页数据
        invoiceSizeChange(val) {
            var that = this;
            that.indentSearch.limit = val;
            // 查询数据
            that.loadInvoiceDatas();
        },
        invoiceCurrentChange(val) {
            // 设置页数
            var that = this;
            that.indentSearch.pageIndex = val;
            that.loadInvoiceDatas();
        },
        showInvDetail(invoiceId) {
            let that = this;
            that.detailDialog.show = true;
            that.detailDialog.invoiceId = invoiceId;
        },
        showExpendDialog(invoice) {
            let that = this;
            that.invExpendDialog.show = true;
            that.invExpendDialog.invoice = invoice;
        },
    }
})
